package com.bootdo.wx.controller;

import java.util.List;
import java.util.Map;
import java.util.Date;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.wx.domain.ShopExpressMoneyDO;
import com.bootdo.wx.service.ShopExpressMoneyService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UUIDUtils;

/**
 * 
 * 
 * @author long
 * @email 499626561@qq.com
 * @date 2020-06-19 13:36:15
 */
 
@Controller
@RequestMapping("/wx/shopExpressMoney")
public class ShopExpressMoneyController {
	@Autowired
	private ShopExpressMoneyService shopExpressMoneyService;
	
	@GetMapping()
	@RequiresPermissions("wx:shopExpressMoney:shopExpressMoney")
	String ShopExpressMoney(){
	    return "wx/shopExpressMoney/shopExpressMoney";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:shopExpressMoney:shopExpressMoney")
	public PageUtils list(@RequestParam Map<String, Object> params){
	    params.put("delFlag","0");
	    params.put("sort","ca_at");
	    params.put("order","desc");
		//查询列表数据
        Query query = new Query(params);
        if(params.containsKey("name")&&null!=params.get("name")&&!"".endsWith(params.get("name").toString())){
        	List<ShopExpressMoneyDO> shopExpressMoneyList = shopExpressMoneyService.likeList(query);
    		int total = shopExpressMoneyList.size();
    		PageUtils pageUtils = new PageUtils(shopExpressMoneyList, total);
    		return pageUtils;
        }
		List<ShopExpressMoneyDO> shopExpressMoneyList = shopExpressMoneyService.list(query);
		int total = shopExpressMoneyService.count(query);
		PageUtils pageUtils = new PageUtils(shopExpressMoneyList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:shopExpressMoney:add")
	String add(){
	    return "wx/shopExpressMoney/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:shopExpressMoney:edit")
	String edit(@PathVariable("id") String id,Model model){
		ShopExpressMoneyDO shopExpressMoney = shopExpressMoneyService.get(id);
		model.addAttribute("shopExpressMoney", shopExpressMoney);
	    return "wx/shopExpressMoney/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:shopExpressMoney:add")
	public R save( ShopExpressMoneyDO shopExpressMoney){
	    shopExpressMoney.setId(UUIDUtils.getUUID());
	    shopExpressMoney.setDelFlag(0);
	    shopExpressMoney.setCaAt(new Date());
		if(shopExpressMoneyService.save(shopExpressMoney)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:shopExpressMoney:edit")
	public R update( ShopExpressMoneyDO shopExpressMoney){
		shopExpressMoney.setUpAt(new Date());
		shopExpressMoneyService.update(shopExpressMoney);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:shopExpressMoney:remove")
	public R remove( String id){
		ShopExpressMoneyDO shopExpressMoney = shopExpressMoneyService.get(id);
		shopExpressMoney.setDelFlag(1);
		shopExpressMoney.setUpAt(new Date());
		if(shopExpressMoneyService.update(shopExpressMoney)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:shopExpressMoney:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
	   if(ids.length>0){
			for (String string : ids) {
				ShopExpressMoneyDO shopExpressMoney = shopExpressMoneyService.get(string);
				shopExpressMoney.setDelFlag(1);
				shopExpressMoney.setUpAt(new Date());
				shopExpressMoneyService.update(shopExpressMoney);
			}
		}
	
		return R.ok();
	}
	
}
